package com.ibm.workplace.wcm.cqplugin;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.List;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.*;
import org.eclipse.jface.dialogs.*;
import org.eclipse.jface.resource.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.plugin.*;
import org.osgi.framework.*;

import com.ibm.workplace.wcm.cqplugin.IBugQueryStore;
import com.ibm.collaboration.realtime.*;
//import com.ibm.collaboration.realtime.im.community.*;
import com.ibm.collaboration.realtime.notifications.*;
import com.ibm.collaboration.realtime.people.*;
//import com.ibm.collaboration.realtime.servicehub.*;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.ibm.workplace.wcm.cqplugin.cqweb.*;
import com.ibm.workplace.wcm.cqplugin.ui.dialogs.BugViewDialog;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "CQplugin";

	// The shared instance
	private static Activator plugin;
	
    public static final String IMAGE_BUG = "icons/bug.gif";
    public static final String IMAGE_BUG_VIEW = "icons/bugnew.gif";
    public static final String IMAGE_FIND = "icons/find.png";
    public static final String IMAGE_PROPERTY = "icons/property.gif";
    public static final String IMAGE_REFRESH = "icons/refresh.gif";
    public static final String IMAGE_QUERY = "icons/query2.gif";
    public static final String IMAGE_STOP = "icons/stop.gif";
    public static final String IMAGE_ASSIGN = "icons/assign.gif";
    public static final String IMAGE_ADD_COMMENT = "icons/addcomment.gif";

    public static final String HATJ_CONF_NAME = "httptest-apache.properties";
    public static final String CQ_CONF_NAME = "setup/CQPlugin.properties";
    
    public static final boolean DEBUG_ON = false;
    
    public static InputStream HATJ_CONF;
    public static InputStream CQ_CONF;
    
    public static CQHttpTest cqTest;
	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
        registerImages();
        
        registerCQConf();
        
        //init CQHttpTest;
        cqTest = new CQHttpTest();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
		
		//release CQ
		cqTest = null;
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}
	
	public static CQHttpTest getCQTest() {
		return cqTest;
	}
	
    private void registerImages() {
        getImageRegistry().put(IMAGE_BUG, ImageDescriptor.createFromURL(getBundle().getEntry(IMAGE_BUG)));
        getImageRegistry().put(IMAGE_BUG_VIEW, ImageDescriptor.createFromURL(getBundle().getEntry(IMAGE_BUG_VIEW)));
        getImageRegistry().put(IMAGE_FIND, ImageDescriptor.createFromURL(getBundle().getEntry(IMAGE_FIND)));
        getImageRegistry().put(IMAGE_PROPERTY, ImageDescriptor.createFromURL(getBundle().getEntry(IMAGE_PROPERTY)));
        getImageRegistry().put(IMAGE_REFRESH, ImageDescriptor.createFromURL(getBundle().getEntry(IMAGE_REFRESH)));
        getImageRegistry().put(IMAGE_QUERY, ImageDescriptor.createFromURL(getBundle().getEntry(IMAGE_QUERY)));
        getImageRegistry().put(IMAGE_STOP, ImageDescriptor.createFromURL(getBundle().getEntry(IMAGE_STOP)));
        getImageRegistry().put(IMAGE_ASSIGN, ImageDescriptor.createFromURL(getBundle().getEntry(IMAGE_ASSIGN)));
        getImageRegistry().put(IMAGE_ADD_COMMENT, ImageDescriptor.createFromURL(getBundle().getEntry(IMAGE_ADD_COMMENT)));
    }

    public static Image getImage(String imageName) {
        Image ret = getDefault().getImageRegistry().get(imageName);
        if (ret == null) {
            getDefault().getImageRegistry().put(imageName,  ImageDescriptor.createFromURL(getDefault().getBundle().getEntry(imageName)));
            ret = getDefault().getImageRegistry().get(imageName);
        } 
        
        return ret;
    }
    
    private void registerCQConf() {
    	
    	try
    	{
    		//bundle CQ conf
    		Bundle bundle = Platform.getBundle(PLUGIN_ID);
		    org.eclipse.core.runtime.Path path = new org.eclipse.core.runtime.Path(this.CQ_CONF_NAME);
		    URL fileURL1 = FileLocator.find(bundle, path, null);
		    CQ_CONF = fileURL1.openStream();
		    
    		//bundle CQ conf
    		//Bundle bundle = Platform.getBundle(PLUGIN_ID);
		    path = new org.eclipse.core.runtime.Path(this.HATJ_CONF_NAME);
		    URL fileURL2 = FileLocator.find(bundle, path, null);
		    HATJ_CONF = fileURL2.openStream();		    
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}

    }
    private BugViewDialog bugViewDialog;
    
    public BugViewDialog showBug(Shell parentShell, IBugInfo bug) {
        return showBugs(parentShell, new IBugInfo[] {  bug } );
    }

    public BugViewDialog showBugs(Shell parentShell, IBugInfo[] bugs) {
        if (bugViewDialog == null || bugViewDialog.getShell() == null || bugViewDialog.getShell().isDisposed()) {
            bugViewDialog = new BugViewDialog(parentShell);            
            bugViewDialog.setBlockOnOpen(false);
            bugViewDialog.open();
        }        
        bugViewDialog.getShell().setActive();        
        bugViewDialog.showBugs(bugs);        

        return bugViewDialog;
    }
    

}
